package com.meida.student.app.user.handler;


import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.RedisUtils;
import com.meida.module.bus.client.enums.BalanceLogTypeEnum;
import com.meida.module.pay.provider.service.PayBalanceLogService;
import com.meida.module.user.client.entity.AppUser;
import com.meida.module.user.provider.service.AppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 邀请记录
 */
@Component
public class AppUserInviteListHandler implements PageInterceptor<AppUser> {

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private PayBalanceLogService payBalanceLogService;


    @Override
    public void prepare(CriteriaQuery<AppUser> cq, PageParams pageParams, EntityMap params) {
        Long userId = OpenHelper.getUserId();
        cq.clear();
        cq.select(AppUser.class, "userId", "userNo", "userName", "nickName", "avatar");
        cq.eq(AppUser.class, "inviterId", userId);
    }

    @Override
    public void complete(CriteriaQuery<AppUser> cq, List<EntityMap> result, EntityMap extra) {
        Long userId = OpenHelper.getUserId();
        long inviterNum = appUserService.countByInviterId(userId);
        AppUser appUser = appUserService.getUserById(userId);
        extra.put("inviterNum", inviterNum);
        String invitationDesc = redisUtils.getConfig("INVITATION_CONFIG");
        extra.put("invitationDesc", invitationDesc);
        extra.put("seatNum", FlymeUtils.getInteger(appUser.getBusNum(), 0));
        BigDecimal sumInviterBalance = payBalanceLogService.totalAmountByLogType(userId, BalanceLogTypeEnum.logType6.getValue());
        extra.put("sumInviterBalance", sumInviterBalance);
    }
}
